package com.topgay.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.topgay.domain.ChattingMessage;
import com.topgay.domain.Member;
import com.topgay.service.ChattingService;
import com.topgay.service.MemberService;

@Controller
@RequestMapping("/chatting/")
public class ChattingController {
	
	@Autowired
	private ChattingService service;
	
	@Autowired
	private  MemberService memberService;

	@RequestMapping("timeLineRequest.do")
	public String timeLineReqeust(HttpSession session){
		return "chatting/request_timeline";
	}
	/**
	 * Time Line 화면을 띄워줌 (새 창)
	 * @param fromId, toId (상대방과 내 아이디)
	 * @return Time Line View
	 * @throws SQLException 
	 */
	@RequestMapping("chattingIndex.do")
	public ModelAndView showChattingIndex(String toId, HttpSession session) throws SQLException{
		ModelAndView av = new ModelAndView();
		
		Member member = null;
		if(toId != null && toId.trim().length() != 0){//별명
			member = memberService.getMemberById(toId);
		}else{
			//friendId && friendNickname 모두 null
		}
		session.setAttribute("member_info", member);
		
		av.setViewName("chatting/chatting");
		
		return av;
	}
	/**
	 * Message 전송
	 * @param ChattingMessage(picturePath는 null이다.)
	 * @return ChattingMessage
	 * @throws Exception
	 */
	@RequestMapping("sendMessage.do")
	public String sendMessage(ChattingMessage message, ModelMap map) throws Exception{
		message.setTime(service.sendMessage(message).getTime());
		return "jsonView";
	}
	/**
	 * 사진 전송
	 * @param ChattingMessage(message는 null이다.), MultipartFile(사진정보)
	 * @return ChattingMessage(사진경로 포함)
	 * @throws Exception
	 */
	@RequestMapping("sendPicture.do")
	public String sendPicture(ChattingMessage message, @RequestParam(value="picture")MultipartFile picture, ModelMap map) throws Exception{
		message.setTime(service.sendPicture(message, picture).getTime());
	
		return "jsonView";
	}
	/**
	 * TimeLine접속 했을 때 한 번 message들을 긁어 온다.
	 * @param ChattingMessage(message는 null이다.), MultipartFile(사진정보)
	 * @return ChattingMessage(사진경로 포함)
	 */
	@RequestMapping("firstGet.do")
	public String firstGetMessage(String fromId, String toId, ModelMap map){
		List<ChattingMessage> list=service.selectTimeLineMessageById(fromId, toId);
		
		map.addAttribute("list", list);
		
		return "jsonView";
	}
	/**
	 * 2초 간격으로 DB에 내게 보낸 메세지를 가져온다.
	 * @param ChattingMessage(message는 null이다.), MultipartFile(사진정보)
	 * @return ChattingMessage(사진경로 포함)
	 */
	@RequestMapping("realTimeGet.do")
	public String realTimeGetMessage(String fromId, String toId, ModelMap map){
		List<ChattingMessage> list = service.selectChattingMessageById(fromId, toId);
		map.addAttribute("list", list);

		return "jsonView";
	}
	/**
	 * 맨 처음 타임라인이 생성될때 실행한다.
	 * @param fromId, toId
	 * @return 생성 후 Time Line을 띄워줌
	 */
	@RequestMapping("makeTimeLineSeq.do")
	public String makeTimeLineSeq(HttpSession session, String friendId){
		String toId = friendId;
		String fromId = ((Member)session.getAttribute("login_info")).getId();
		service.makeTimeLineSeq(fromId,toId);
		
		return "redirect:chattingIndex.do?toId="+toId;
	}
}
